package com.quwan.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quwan.app.service.PrePaymentInterface;
import com.quwan.app.service.VipService;
import com.quwan.app.vo.pay.BalancePrePayment;
import com.quwan.app.vo.pay.PrePayParams;
import com.quwan.app.vo.pay.PrePayment;
import com.quwan.app.vo.pay.WxPrePayment;
import com.quwan.common.BaseService;
import com.quwan.common.enums.CommonStatus;
import com.quwan.config.yml.OssConfig;
import com.quwan.entity.TRechargeVip;
import com.quwan.entity.TVipRights;
import com.quwan.entity.TVipRule;
import com.quwan.mapper.TRechargeVipMapper;
import com.quwan.mapper.TVipRightsMapper;
import com.quwan.mapper.TVipRuleMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author quan
 * @date 2022-04-25 17:57
 */
@Service
@RequiredArgsConstructor
public class VipServiceImpl extends BaseService implements VipService {
    private final OssConfig ossConfig;
    private final TVipRightsMapper rightsMapper;
    private final TVipRuleMapper ruleMapper;
    private final TRechargeVipMapper rechargeVipMapper;

    @Override
    public List<TVipRights> getVipRightsList() {
        return rightsMapper
                .selectList(
                        new QueryWrapper<>(
                                new TVipRights()
                                        .setStatus(CommonStatus.NORMAL.getType())
                        ).select("id", "icon", "rights")

                )
                .stream()
                .limit(50)
                .map(rights -> rights.setIcon(StringUtils.isBlank(rights.getIcon()) ? "" : ossConfig.getHostname() + "/" + rights.getIcon()))
                .collect(Collectors.toList());
    }

    @Override
    public List<TVipRule> getVipRuleList() {
        return ruleMapper
                .selectList(
                        new QueryWrapper<>(
                                new TVipRule()
                                .setStatus(CommonStatus.NORMAL.getType())
                        )
                        .select("id","price","origin_price","remarks","unit")
                        .orderByAsc("price")

                );
    }


    /**
     * vip 预支付信息保存
     *
     * @param prePayParams
     * @param mId
     * @param payMethod
     * @param vipId
     * @param paymentInterface
     * @return
     */
    @Override
    public PrePayment saveVipPrepayInfo(PrePayParams prePayParams, String mId, String payMethod, String vipId,
                                         PrePaymentInterface paymentInterface) {
        TRechargeVip record = new TRechargeVip();
        PrePayment prePayment = paymentInterface.prePayment(prePayParams);
        record
                .setRechargeSlipNum(prePayParams.getOrderId())
                .setMId(Long.parseLong(mId))
                .setMoney(new BigDecimal(prePayParams.getMoney()))
                .setPayMethod(Integer.parseInt(payMethod))
                .setVipId(Long.parseLong(vipId));

        if (prePayment instanceof WxPrePayment)
            record.setPrePayId(((WxPrePayment) prePayment).getPrePayId());
        else if (prePayment instanceof BalancePrePayment)
            record.setPrePayId(((BalancePrePayment) prePayment).getPreId());
        Assert.state(rechargeVipMapper.insert(record) == 1, "save recharge vip  info exception");
        return prePayment;

    }
}
